/*************************************************************************
 *************************************************************************
 	 	 	 	 	 	 	M2SMA : SDLAgent.h
 *************************************************************************
 *
 	 Copyright (C) 2011
 	 DW-2011, Steven Costiou, France
 *
 **************************************************************************
 **************************************************************************
 *
	This is the top level agent for graphic management based on SDL.
	It manages the application screen and implements features to display
	graphic objects on it.

	It handles requests to its services featuring screen display and other
	graphic operations.

	Basically, any object requesting a display will be stored in proper
	structures until the real display in the live() method, where every
	stored object will be displayed.
 *
 **************************************************************************
 **************************************************************************
 *
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/
 *
 **************************************************************************
 **************************************************************************/

#ifndef SDLAGENT_H_
#define SDLAGENT_H_

#include "Agent.h"
#include "DWGraphicAgent.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <vector>
#include <map>

class SDLAgent : public Agent
{
	public:

		SDLAgent(Engine* parentEngine);
		~SDLAgent();

		SDL_Surface* getScreen() { return this->screen; };
		SDL_Rect* getBounds() { return this->bounds; };

		/**
		 * Services interfaces.
		 */

		/**
		 * Stores a white rectangle that is requested to
		 * be displayed on the screen.
		 */
		void displayWhiteRectangle(SDL_Rect* anSDLRectangle);

		/**
		 * Registers a graphic agent to be displayed each time
		 * the screen is updated. Returns the id of the registration
		 * if it has been successfully performed, 0 if it failed.
		 */
		unsigned long int registerAgentForDisplay(DWGraphicAgent* aDWGraphicAgent); /// TODO change Agent* to super graphic agent pointer

		/**
		 * Unregisters a graphic agent, which will never be displayed anymore.
		 * Note that the destruction of the agent is not performed, as it is not
		 * Responsibility of the graphic engine to do so.
		 */
		void unregisterAgentFromDisplayList(unsigned long int regId);

		virtual void live();

	protected:

		SDLAgent(const SDLAgent& anSDLAgent);

		virtual bool initialize();
		virtual void clean_up();
		virtual void startAgent();

	private:

		bool agentStarted;

		int SCREEN_WIDTH;
		int SCREEN_HEIGHT;
		int SCREEN_BPP;

		SDL_Rect* bounds;

		SDL_Surface* screen;

		SDL_Event event;

		vector<SDL_Rect*>* toDisplayList;
		vector<SDL_Rect*>::iterator toDisplayObjectIterator;

		/**
		 * TODO validate the following.
		 */
		/**
		 * Starts at 1. Counts the number of registered agents for display and
		 * is usefull to assign a registration id to each agent.
		 */
		unsigned long int registeredAgents;

		map<unsigned long int, DWGraphicAgent*>* registeredAgentsForDisplay;
		map<unsigned long int, DWGraphicAgent*>::iterator toDisplayGraphicsObjectIterator;

};

#endif /* SDLAGENT_H_ */
